package com.tyrant.houtai;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;
import com.tyrant.houtai.bean.InfoBean;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GlobalActivity extends AppCompatActivity {

    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.textView3)
    TextView textView3;
    @Bind(R.id.textView4)
    TextView textView4;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.editText2)
    EditText editText2;
    @Bind(R.id.editText3)
    EditText editText3;
    @Bind(R.id.editText4)
    EditText editText4;
    @Bind(R.id.btn_save)
    Button btn_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_global);
        ButterKnife.bind(this);

        getCustomerMsg();
    }

    private void getCustomerMsg() {
        new HttpUtil.Builder("customerService/backGround/getInfoFromBg")
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        InfoBean contactUsBean = new Gson().fromJson(model, InfoBean.class);
                        if (contactUsBean.getFlag() == 1) {
                            String mobile = contactUsBean.getResult().getMobile();
                            editText.setText(mobile);
                            String qq = contactUsBean.getResult().getQq();
                            editText2.setText(qq);
                            String authorization_code = contactUsBean.getResult().getAuthorization_code();
                            editText3.setText(authorization_code);
                            String url = contactUsBean.getResult().getUrl();
                            editText4.setText(url);
                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .get();
    }

    private void saveInfo() {
        new HttpUtil.Builder("customerService/setInfo")
                .Params("mobile", editText.getText().toString())
                .Params("qq", editText2.getText().toString())
                .Params("authorizationCode", editText3.getText().toString())
                .Params("url", editText4.getText().toString())
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        InfoBean contactUsBean = new Gson().fromJson(model, InfoBean.class);
                        if (contactUsBean.getFlag() == 1) {

                            Toast.makeText(GlobalActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(GlobalActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .post();
    }

    @OnClick(R.id.btn_save)
    public void onClick() {
        saveInfo();
    }
}
